
var sendtext = "&id=75";
var url="xml?method=gw.config.language"+sendtext+'&tmp='+Math.random();

$.get(url,function (data) {
    var stat=$(data).find("rsp").attr("stat");
    if(stat=="fail") {
        $(data).find('Error').each(function () {
            var code=$(this).attr('code');
            if(code==4) {
                alert(cannotModify);
            }else if(code==5) {
                gotologin();
            }
        });
    }else{
        var flag = $(data).find("item").attr("value");
        if(flag=="0"){
            document.title="WROC";
            $("#logo").removeClass("logoST").addClass("logo");
        }else{
                document.title=msg.webTitle;

            $("#logo").removeClass("logo").addClass("logoST");
        }

    }
});

var modules = {
	"none":"",
  "get_json":"json?method=gw.config.get",
 "get_data" : "xml?method=gw.config.get",
 "set_data" : "xml?method=gw.config.set",
 "batch" : "xml?method=gw.config.copy",
 "system_reboot" : "xml?method=gw.system.reboot",
 "app_reboot" : "xml?method=gw.app.reboot",
 "config_reset" : "xml?mthod=gw.config.reset",
 "config_default" : "xml?method=gw.config.default",
 "data_download" : "xml?method=gw.config.download",
 "login" : "xml?method=gw.account.login",
 "change_password" : "xml?method=gw.account.change",
 "logout" : "xml?method=gw.config.logout",
 "log_get" : "xml?method=gw.log.get",
 "log_download" : "xml?method=gw.log.download",
 "log_clear" : "xml?method=gw.log.clear",
 "status_get" : "xml?method=gw.status.get",
 "language_get" : "xml?method=gw.config.language",
 "command_set" : "xml?method=gw.command.set",
 "profile_get" : "xml?method=gw.profile.get",
 "profile_set" : "xml?method=gw.profile.set",
 "profile_remove" : "xml?method=gw.profile.remove"
}

var adm=getCookie("admin");
/*if(adm=="admin"){
	$("#loginuser").html(share.admin);
	}else if(adm=="operator"){
		$("#loginuser").html(share.oper);
	}else if(adm=="view"){
		$("#loginuser").html(share.view);
	}
	var sendtext = "&id=69";
	var url=modules["get_data"]+sendtext+'&tmp='+Math.random();
	$.get(url,function (data) {
		var stat=$(data).find("rsp").attr("stat");
		if(stat=="fail") {
			$(data).find('Error').each(function () {
				var code=$(this).attr('code');
				if(code==4) {
					alert(cannotModify);
				}else if(code==5) {
					gotologin();
				}
			});
		}else{
			$(data).find("item").each(function(){
    			if($(this).attr("id")=="69"){
    				var uptime= $(this).attr("value");
					$("#uptime").text(uptime);
    			}
    		});
		}
	});
	*/
	
$("input").each(function(){                                      
if($(this).attr("readonly")){                                 
$(this).css("background-color","#cccccc");                        
}                                                                 
}); 
	
$("form").submit(function(){
	return false;
	});
	
function reloads(){
	window.location.reload();
	}

function DataRequest(module_name,param,callback){
if(module_name.indexOf("cgi-bin")>=0||module_name.indexOf("status?get&param")>=0){
 	this.url  =  module_name; 
 this.callback  =  callback;
 var _this  =  this;
 $.get(this.url,"tmp"+Math.random(),function(data){
  try{
  	//data = data;
   data  = eval('('+ data+')');
  }
  catch(e){
  //gotologin();
 }

   if(_this.callback){
   _this.callback(data);
  }
 });
}else{
	 this.url  =  modules[module_name]+param;
	 this.callback  =  callback;
 var _this  =  this;
 $.get(this.url,{"tmp":Math.random()},function(data){
  try{
  	//data = data;
   data  = eval('('+ data+')');
  }
  catch(e){
  //gotologin();
 }
   if(_this.callback){
   _this.callback(data);
  }
 });
	}

}

function xmlDataRequest(module_name,param,callback){
 this.url  =  modules[module_name]+param;
 this.callback  =  callback;
 var _this  =  this;
 $.get(this.url,function(data){
	var stat=$(data).find("rsp").attr("stat");
	if(stat=="ok"){
		data=data;
	}else{
		var errorcode=$(data).find("Error").attr("code");
		if(errorcode=="5"){
			gotologin();
			}
	}
   if(_this.callback){
   _this.callback(data);
  }
 });
}


function openHelpWindow(url) {
	var top  =  30;
	var left  =  Math.floor(screen.availWidth * .66) - 10;
	var width  =  Math.floor(screen.availWidth * .33);
	var height  =  Math.floor(screen.availHeight * .9) - 30;
	var win  =  window.open(url, 'WIFI_Help', 'top = ' + top + ',left = ' + left + ',width = ' + width + ',height = ' + height + ",resizable = yes,scrollbars = yes,statusbar = no");
	win.focus();
}


function submitform(surl,settext,urlId) {
	show_message("pro",msg.saving);
	var cannotModify  =  msg.cannotModify1;
	var noPower  =  msg.cannotModify2;
	var success  =  msg.submitSuccess;
	var reboot = msg.restartSuccess;
	var cannotDelete = msg.cannotDelete;
	var cfgError = msg.cfgError;

    $.ajax({
		type:"POST",url:surl,data:settext+"&tmp = "+Math.random(),timeout:25000,dataType:"xml",error:function (XMLHttpRequest,textStatus,errorThrown) {
			window.location.reload();
		},success:function (xml) {
			$(xml).find('rsp').each(function () {
				$("#message_panel").fadeOut();
  				$("#lock_div").hide();
  				$("#lock_div2").hide();
  				$("#message_panel").hide();
				var stat = $(this).attr('stat');

                if(stat == "ok") {
						//show_message("success",success);
						alert(success);
                    if(typeof(urlId)!="undefined"){
                        var showHtm = urlId + ".htm";
                        loadNewHtml(showHtm);
                    }

				}else if(stat == "restart"||stat == "reboot") {
						alert(reboot);
                    if(typeof(urlId)!="undefined"){
                        var showHtm = urlId + ".htm";
                        loadNewHtml(showHtm);
                    }
				}else if(stat == "fail") {
					//show_message("err",msg.cfgError);
					var code = $(xml).find('Error').attr('code');
						if(code == 4) {
							//show_message("err",cannotModify);
							alert(cannotModify);
						}else if(code == 5) {
							gotologin();
						}else {
							if($("[name = group]").attr("name")) {
								var str = '';
								$(xml).find('Error').each(function () {
										var strArray = $(this).attr('msg').split(" ");
										var mid = strArray[1].substr(2);
										str += mid+",";
									
								});
								//show_message("err",cannotDelete);
								alert(cannotDelete);
							}else if($("[name = tra-dis]").attr("name")) {
								//show_message("err",dialingRules.help5);
								alert(dialingRules.help5);
							}else {
								var strArray = $(xml).find('Error').attr('msg').split(" ");
								var mid = strArray[1].substr(2);
								if(mid == 32||mid == 31||mid == 33||mid == 34) {
									//show_message("err",cfgError);
									alert(cfgError);
								}else if($(xml).find('Error').attr('msg').indexOf("line_id")>=0){
									var str1=$(xml).find('Error').attr('msg').split(",");
									var str2=str1[1].split(" ");
									var line_id=str2[1].split("=")[1];
                                    if($("#msg"+mid+"_"+line_id).attr("id")){
									$("#msg"+mid+"_"+line_id).html('<img src="images/check_error.gif">');
                                    }else{
                                        showerror(mid);
                                    }
								}else{
									alert(msg.cfgError);
									showerror(mid);
								}
							}
						}
				}
			});
		}
	});
}

function checks() {
	var resultloop="";
	$.ajax({
		type:"POST",async:false,url:'/xml?method=gw.command.set&action=check&tmp='+Math.random(),dataType:"xml",error:function (xml) {
			$(xml).find('Error').each(function () {
				var errortext=$(this).attr('msg');
			});
		},success:function (xml) {
			var stat=$(xml).find('rsp').attr("stat");
			if(stat=="ok") {
				var result=$(xml).find('Result').text();
				if($.trim(result)!=="OK") {
					if(result.indexOf("WARNING")>=0) {
						var result_array=result.split("\n");
						resultloop+=checkfeature.cue1+"\r\n";
						for(var i=0;i<result_array.length;i++) {
							if(result_array[i]!==""&&result_array[i].indexOf(":")>=0) {
								var str1='';
								var valueloop=result_array[i].split(":")[1].split("is conflict with");
								if(valueloop[0].indexOf("PREFIX_")>=0) {
									str1=$.trim(dialingRules.table1_td1+valueloop[0].split(" ")[2]);
								}else if(valueloop[0].indexOf("EXT-")>=0) {
									str1=$.trim(valueloop[0].split("(")[0].replace("EXT-",dialingRules.phone));
								}else if(valueloop[0].indexOf("TIE EXT PREFIX")>=0) {
									str1=$.trim(valueloop[0].split("(")[0].replace("TIE EXT PREFIX",dialingRules.extprefix));
								}else if(valueloop[0].indexOf("TIE OUTGOING PREFIX")>=0) {
									str1=$.trim(valueloop[0].split("(")[0].replace("TIE OUTGOING PREFIX",dialingRules.outprefix));
								}else if(valueloop[0].indexOf("FEATURE CODE")>=0) {
									str1=$.trim(valueloop[0].split("(")[0].replace("FEATURE CODE",ivr_menu.featurecfg));
								}var str2='';
								if(valueloop[1].indexOf("PREFIX_")>=0) {
									str2=$.trim(dialingRules.table1_td1+valueloop[1].split(" ")[2]);
								}else if(valueloop[1].indexOf("EXT-")>=0) {
									str2=$.trim(valueloop[1].split("(")[0].replace("EXT-",dialingRules.phone));
								}else if(valueloop[1].indexOf("TIE EXT PREFIX")>=0) {
									str2=$.trim(valueloop[1].split("(")[0].replace("TIE EXT PREFIX",dialingRules.extprefix));
								}else if(valueloop[1].indexOf("TIE OUTGOING PREFIX")>=0) {
									str2=$.trim(valueloop[1].split("(")[0].replace("TIE OUTGOING PREFIX",dialingRules.outprefix));
								}else if(valueloop[1].indexOf("FEATURE CODE")>=0) {
									str2=$.trim(valueloop[1].split("(")[0].replace("FEATURE CODE",ivr_menu.featurecfg));
								}
								resultloop+=i+"."+str1+$(allXml).find("jsmsg and").text()+str2+msg.existed+"\r\n";
							}
						}
						resultloop+="\r\n"+Captrue(checkfeature.cue2);
					}
				}
			}
		}
	});
	return resultloop;
}


function unique( array ) {
	var ret = [], done = {};
	try {
		for ( var i = 0, length = array.length; i < length; i++ ) {
			var id = array[ i ];
			if ( !done[ id ] ) {
				done[ id ] = true;
				ret.push( array[ i ] );
			}
		}
	} catch( e ) {
		ret = array;
	}
	return ret;
}

function checkname(nonames,cname){
	for (loop = 0; loop < nonames.length; loop++){
		if(nonames[loop]==cname){
			return false;	
		}
	}
		return true;
}

function sendinfo(sendtext,nonames){
	DataRequest("get_json",sendtext,function(data){
  		getResult(nonames,data);
  });
}

function getResult(nonames,data){
	$(":input").each(function(){
		if(checkname(nonames,$(this).attr('name'))) {
			updatecfgvalue($(this).attr('name'),data[$(this).attr('name')]);
		}
	});
}

function getSendText(nonames){
	var gettext = '';
	$(":input").each(function(){
		if(checkname(nonames,$(this).attr('name'))) {
			if($.trim($(this).attr('name')).substr(2)&&$.trim($(this).attr('name')).substr(2)!=""){
				gettext+="&id="+$.trim($(this).attr('name')).substr(2);
			}
		}
	});
	return gettext;
}

function getSetText(nonames){
	var settext = '';
	$(":input").each(function(){
		if(checkname(nonames,$(this).attr('name'))) {
			if($(this).attr("name")!=""&&$(this).attr("name")){
			if($(this).attr('type')=='checkbox') {
					settext+="&"+$(this).attr('name')+"="+gettruevalue($(this).attr('name'),$.trim($(this).val()));
			}else if($(this).attr('type')=='radio'){
				if($(this).attr('checked')){
					settext+="&"+$(this).attr('name')+"="+$.trim($(this).val());
				}
			}else{
				settext+="&"+$(this).attr('name')+"="+$.trim($(this).val());
			}
		}
		}
	});
	return settext;
}


function updatecfgvalue(cofigid,cfgvalue){
	if($("input[name="+cofigid+"]").attr('type')=='radio')
	{
		if(cfgvalue!=""){
			$("input[name="+cofigid+"]").val([cfgvalue]);
		}
	}else if($("input[name="+cofigid+"]").attr('type')=='checkbox'){
		showcheckboxvalue(cofigid,cfgvalue);
	}else{
		if(cfgvalue==""&&(cofigid=="2008"||cofigid=="2009")){
			$("input[name="+cofigid+"]").attr("value","180");
			}else{
			$("input[name="+cofigid+"]").val([cfgvalue]);
		}
	}


	$("select[name="+cofigid+"]").find('option').each(function(){

        var opvalue='';
		if($(this).val()==''){
			opvalue=$(this).text();
		}else{opvalue=$(this).val();}
        if(typeof(cfgvalue)=="undefined"){
            return false;
        }
		var newcfgvalue=cfgvalue.toUpperCase();
		var opvalue=opvalue.toUpperCase();
		if(newcfgvalue==opvalue){
			$(this).attr('selected',true);
		}
	});

}

function listselect(ID,cfgvalue){
    $("#"+ID).find('option').each(function(){
        var opvalue=$(this).val();
        var newcfgvalue=cfgvalue;
        if(newcfgvalue==opvalue){
            $(this).attr('selected',true);
        }
    });
}

function showcheckboxvalue(cfgname,tmpvalue){
	var lowertmpvalue=tmpvalue.toLowerCase();
	if(cfgname=="id457"){
		if(lowertmpvalue=='off'||lowertmpvalue=='no'||lowertmpvalue=='0'||lowertmpvalue=='0x00000000'){
			$("input[name="+cfgname+"]").attr('checked',true);
			$("input[name="+cfgname+"]").attr('value',lowertmpvalue);
		}
		if(lowertmpvalue=='on'||lowertmpvalue=='yes'||lowertmpvalue=='1'||lowertmpvalue=='0x00000008'){
			$("input[name="+cfgname+"]").attr('checked',false);
			$("input[name="+cfgname+"]").attr('value',lowertmpvalue);
		}
	}else{
		if(lowertmpvalue=='on'||lowertmpvalue=='yes'||lowertmpvalue=='1'||lowertmpvalue=='0x00000008'){
			$("input[name="+cfgname+"]").attr('checked',true);
			$("input[name="+cfgname+"]").attr('value',lowertmpvalue);
		}
		if(lowertmpvalue=='off'||lowertmpvalue=='no'||lowertmpvalue=='0'||lowertmpvalue=='0x00000000'){
			$("input[name="+cfgname+"]").attr('checked',false);
			$("input[name="+cfgname+"]").attr('value',lowertmpvalue);
		}
	}
}

function gettruevalue(cfgname,tmpvalue){
	if(tmpvalue.toLowerCase()=='on'||tmpvalue.toLowerCase()=='off'){
		if(cfgname=="id457"){
			if($("input[name="+cfgname+"]").attr('checked')){
				var truevalue="off";
			}else{
				var truevalue="on";
			}
		}else{
			if($("input[name="+cfgname+"]").attr('checked')){
				var truevalue="on";
			}else{
					var truevalue="off";
			}
		}
	}else if(tmpvalue.toLowerCase()=='yes'||tmpvalue.toLowerCase()=='no'){
		if($("input[name="+cfgname+"]").attr('checked')){
			var truevalue='yes';
		}else{
			var truevalue='no';
		}
	}else if(tmpvalue=='1'||tmpvalue=='0'){
		if($("input[name="+cfgname+"]").attr('checked')){
			var truevalue='1';
		}else{
			var truevalue='0';
		}
	}else if(tmpvalue=='0x00000008'||tmpvalue=='0x00000000'){
		if($("input[name="+cfgname+"]").attr('checked')){
			var truevalue='0x00000008';
		}else{
			var truevalue='0x00000000';
		}
	}
	return truevalue;
}

function gotologin(){
	$("body").append("<form name=logout target=_top action=index.htm></form><script>document.logout."+"submit();</script>"); 
}

function logout(){
	window.location.href ="login.htm";
}

function isNumber(s)
{
var repPass = /[a-zA-Z~!@$%^&()_+]/;
if (s!=null && s!="")
{
if(!repPass.test(s)){
return true;
}
}
return false;
}

function showerror(mid)
{
var msg=" ";
var imgsrc='<img src="images/check_error.gif">';
var imgid="img"+mid;
var msgid="msg"+mid;
var tdid="td"+mid;
$("#"+msgid).html(msg); 
$("#"+msgid).css("color","red");
$("#"+imgid).html(imgsrc); 
}

function checkDomain(enDomain)
{
var i;
var ii;
var aa;
if(enDomain=="" || enDomain==" " || enDomain.length < 4) {
return false;
}

var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789|.!=+%/_:";
var allValid = true;
for (i = 0;  i < enDomain.length;  i++)
{
ch = enDomain.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

if (!allValid){
return (false);
}


if(enDomain.length>0)
{
ii=enDomain.indexOf(".")
if(ii==-1)
{
return false
}
}
if(enDomain.indexOf(":")>=0){
if(checkIp(enDomain.split(":")[0])){
return false;
}
}else{
if(checkIp(enDomain)){
return false;
}
}
return true; 
}

function isChina(s){ 
var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi; 
if(!patrn.exec(s)){ 
return false; 
}else{ 
return true; 
} 
}

function checkIp(ip){
var ipPattern = new RegExp("^(([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\.((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\.((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\.((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$","g");

var ip6Pattern=new RegExp("^(^::$)|(^([\\d|a-fA-F]{1,4}:){7}([\\d|a-fA-F]{1,4})$)|(^(::(([\\d|a-fA-F]{1,4}):){0,5}([\\d|a-fA-F]{1,4}))$)|(^(([\\d|a-fA-F]{1,4})(:|::)){0,6}([\\d|a-fA-F]{1,4})$)$","g");

if(ipPattern.test(ip)){
return true;
}
if (ip6Pattern.test(ip)){
var pos=ip.indexOf("::");
if (pos>-1)
{
pos=ip.indexOf("::",pos+2);
if (pos>-1) return false;
}
return true;
}
return false;
}

function openSelectLine(buttonId,groupid){
	//$("select").hide();
	$.get("/xml?method=gw.config.get"+groupid+"&tmp="+Math.random(),function (data) {
		if($(data).find("rsp").attr("stat")=="ok") {
			var linehtml='<tr><td class="table_title" colspan=4 id="sb3">'+dialingRules.phone+'<span style="padding-left:50px;"></span><input type="checkbox" name="checklineall"/>&nbsp;'+sbutton.selectall+'</td></tr>';
			var loopvalue=new Array();
			var j=0;
			$(data).find('value').each(function () {
				if($(this).text().split("/")[2]!=='') {
					loopvalue[j]=$(this).text();
					j++;
				}
			});
			for(var x=0;x<loopvalue.length;x+=4) {
				if(x>=loopvalue.length) {
								linehtml+='<tr><td>&nbsp;</td>';
							}else {
								linehtml+='<tr><td><input type=checkbox name="checkid" id="fxs'+loopvalue[x].split("/")[1]+'_'+loopvalue[x].split("/")[2]+'">'+loopvalue[x].split("/")[2]+'</td>';
							}if((x+1)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+1].split("/")[1]+'_'+loopvalue[x+1].split("/")[2]+'">'+loopvalue[x+1].split("/")[2]+'</td>';
							}if((x+2)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+2].split("/")[1]+'_'+loopvalue[x+2].split("/")[2]+'">'+loopvalue[x+2].split("/")[2]+'</td>';
							}if((x+3)>=loopvalue.length) {
								linehtml+='<td>&nbsp;</td></tr>';
							}else {
								linehtml+='<td><input type=checkbox name="checkid" id="fxs'+loopvalue[x+3].split("/")[1]+'_'+loopvalue[x+3].split("/")[2]+'">'+loopvalue[x+3].split("/")[2]+'</td></tr>';
							}
						}
						if(j==0&&(groupid=="4000"||groupid=="4020")) {
							linehtml+='<tr><td colspan=4>'+dialingRules.phoneCfg+'</td></tr>';
						}else if(j==0&&(groupid=="4001"||groupid=="4021")){
							linehtml+='<tr><td colspan=4>'+dialingRules.outPhoneCfg+'</td></tr>';
						}
						linehtml+='<tr><td colspan=4><span style="padding-left:180px;"></span><input type=button name="ok" id="ok" value="'+sbutton.ok+'" class="button"><input type=button name="reset" value="'+sbutton.cancel+'"  class="button"></td></tr>';
						$("#linetest").empty();
						$("#linetest").html(linehtml);
						$("[name=checklineall]").click(function () {
							if($(this).attr("checked")) {
								$("[name=checkid]").attr("checked",true);
							}else {
								$("[name=checkid]").attr("checked",false);
							}
						});
						$("#ok").click(function () {
							$("select").show();
							var lines='';
							var trueline='';
							$(":checkbox").each(function () {
								if($(this).attr("id").indexOf("fxs")>=0&&$(this).attr("checked")) {
									var ids=$(this).attr("id").split("_");
									lines+=ids[0].split("fxs")[1]+",";
									trueline+=ids[1]+",";
								}
							});
							lines=lines.substr(0,lines.length-1);
							trueline=trueline.substr(0,trueline.length-1);
							$("#"+buttonId).attr("value",trueline);
							$("#"+buttonId).attr("truevalue",lines);
							$("#layer").hide();
						});
						$("[name=reset]").click(function () {
							$("select").show();
							$("#layer").hide();
						});
						var linevalue=$("#"+buttonId).attr("truevalue").split(",");
						var linevalue1=$("#"+buttonId).attr("value").split(",");
						for(var j=0;j<=linevalue.length;j++) {
							$("#fxs"+linevalue[j]+"_"+linevalue1[j]).attr("checked",true);
						}
					}else {
						$(data).find('Error').each(function () {
							var code=$(this).attr('code');
							if(code==4) {
								//show_message("err",msg.cannotModify1);
								alert(msg.cannotModify1);
							}else if(code==5) {
								gotologin();
							}else {
								var strArray=$(this).attr('msg').split(" ");
								var mid=strArray[1].substr(2);
								if(mid==401) {
									//show_message("err",msg.lineNumberError);
									alert(msg.lineNumberError);
								}else {
									showerror(mid);
								}
							}
						});
					}
				});
				$("#layer").show();
}

function replacestr(str){
	if(str == "off" ||str == "no"){
		str = sip_trunk.table3_td4_option2;
	}else if(str == "yes"||str =="on"){
		str = sip_trunk.table3_td4_option1;
	}else if(str==""){
		str ="";
	}
	return str;
}

function selectObj_chose_set(obj, val, func){
  var collection = obj;
  if(collection)
	  for (var i=0;i<collection.options.length;i++){
		if (collection.options[i].value == val){
			collection.options[i].selected=true;
		}
	  }
	if(func){
		func(val);
	}
}

function strToJSON(strdata){
	var array1 = new Array();
        if(typeof(strdata)=="undefined"){
            return false;
        }
		if(strdata.indexOf(";") >= 0){
			array1 = strdata.split(";");
		}else{
			array1[0] = strdata;	
		}
		var tmparray=new Array();
		for (var i=0;i < array1.length; i++){
			tmparray[i] = new Array();
				var array2 = array1[i].split(",");
				for(var j = 0;j < array2.length;j++){
					tmparray[i][j] = array2[j];
				}
		}
		return tmparray;
}

function JSONTostr(jsondata){
	var strdata='';
	for(var i =0; i < jsondata.length; i++){
		if(jsondata[i]&&jsondata[i]!=""){
			strdata +=jsondata[i].toString()+";";
		}
	}
	strdata = strdata.substr(0,strdata.length-1);
	return strdata;
}

function Modify_button()
{
	$("#save_button").val(sbutton.modify);
}

function lineshow(lid){
	var url=modules["get_data"]+'&id='+lid+'&tmp='+Math.random();
	$.get(url,function (data) {
		var stat=$(data).find("rsp").attr("stat");
		if(stat=="fail") {
			$(data).find('Error').each(function () {
				var code=$(this).attr('code');
				if(code==4) {
					alert(cannotModify);
				}else if(code==5) {
					gotologin();
				}
			});
		}else{
    			if($.trim($(data).find("item").text())==""){
    				$("#lineshow").hide();
    				if(lid=="4000"){$("[name=phone]").hide();}
    				if(lid=="4001"){$("[name=trunk]").hide();}
    				return false;
    			}
		}
	});
	return true;
}

function inputdomain(nonames, cname) {
    for (loop = 0; loop < nonames.length; loop++) {
        if (nonames[loop] == cname) {
            return true;
        }
    }
    return false;
}

function checkDomain(enDomain) {
    var i;
    var ii;
    var aa;
    if (enDomain == "" || enDomain == " " || enDomain.length < 4) {
        return false;
    }
    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789|.!=+%/_:";
    var allValid = true;
    for (i = 0; i < enDomain.length; i++) {
        ch = enDomain.charAt(i);
        for (j = 0; j < checkOK.length; j++)
        if (ch == checkOK.charAt(j)) break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
    }
    if (!allValid) {
        return (false);
    }
    if (enDomain.length > 0) {
        ii = enDomain.indexOf(".")
        if (ii == -1) {
            return false
        }
    }
    if (enDomain.indexOf(":") >= 0) {
        if (checkIp(enDomain.split(":")[0])) {
            return false;
        }
    } else {
        if (checkIp(enDomain)) {
            return false;
        }
    }
    return true;
}

function RndNum(n){
	var rnd="";
	for(var i=0;i<n;i++){
		rnd+=Math.floor(Math.random()*10);
	}
return rnd;
}

function checkmac(mac){
var reg_name=/[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}/;
if(!reg_name.test(mac)){
return true;
}else{
	return false;
}
}

function reboot() {
    var resys = msg.rebootSys;
    var resysFail = msg.rebootSysFail;
    if (confirm(resys)) {
        $.ajax({
            type: "POST",
            url: modules["system_reboot"] + '&tmp=' + Math.random(),
            dataType: "xml",
            error: function (xml) {
                $(xml).find('Error').each(function () {
                    var errortext = $(this).attr('msg');
                   //show_message("err",errortext);
                   alert(errortext);
                });
            },
            success: function (xml) {
                $(xml).find('rsp').each(function () {
                    var stat = $(this).attr('stat');
                    if (stat == "fail") {
                        var errorcode = $(xml).find('Error').attr('code');
                        if (errorcode == '5') gotologin();
                        else 
                        	//show_message("err",resysFail);
                        	alert(resysFail);
                    } else gotologin();
                });
            }
        });
        return true;
    } else return false;
}

function ip2long ( ip_address )
{
    var output = false;
    if ( ip_address.match ( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) )
    {
      var parts = ip_address.split ( '.' );
      var output = 0;
      output = ( parts [ 0 ] * Math.pow ( 256, 3 ) ) +
               ( parts [ 1 ] * Math.pow ( 256, 2 ) ) +
               ( parts [ 2 ] * Math.pow ( 256, 1 ) ) +
               ( parts [ 3 ] * Math.pow ( 256, 0 ) );
    }
    return output;
}


function long2ip ( proper_address ) 
{
    var output = false;
    if ( !isNaN ( proper_address ) && ( proper_address >= 0 || proper_address <= 4294967295 ) ) 
    {
      output = ((proper_address & 0xff000000) >>> 24) + '.' + 
       ((proper_address & 0xff0000) >>> 16) + '.' + 
       ((proper_address & 0xff00) >>> 8) + '.' + 
       ((proper_address & 0xff)) ;
    }
    return output;
}

function checkNet(slip,slm,swip,swm)
{
lip=ip2long(slip);
lm=ip2long(slm);
wip=ip2long(swip);
wm=ip2long(swm);
if(lip==false || lm==false || wip==false || wm==false)
{
return 1;
}
if(lm>wm)lm=wm;
lnet=lip&lm;
wnet=wip&lm;
if(lnet==wnet){
	return 2;
}else{
	return 3;
}
//alert("netmask:"+long2ip(lm)+"lnet:"+long2ip(lnet)+"wnet:"+long2ip(wnet)); 
}




function loadNewHtml(name){
    $("#mainAndHelp").empty();
    $("#mainAndHelp").load(name, function () {
        $("label").each(function () {
            var currentVal =$(this);
            var values =eval($(this).attr("sid"));
            if(typeof(values)!=="undefined"){
                $(this).html(unCompileJsonVal(decodeURIComponent(values)));
                $(this).rightClick(function(){
                    if(currentVal.attr("sid")) {
                        showEditDialog(currentVal);
                        currentVal.addClass("EditPosition");
                    }
                })

            }

            if($(this).html()=="" && $(this).parent().hasClass("definition")){
                $(this).parent().hide();
                $(this).parent().prev().hide();
            }
        });

        $("button").each(function(){
            var currentButton =$(this);
            var ButtonVal = currentButton.find("label");
            $(this).rightClick(function(){
                if(ButtonVal.attr("sid")) {
                    showEditDialog(ButtonVal);
                    ButtonVal.addClass("EditPosition");
                }
            })
        });


        $("option").each(function(){
            var values =eval($(this).attr("sid"));
            if(typeof(values)!=='undefined'){
                $(this).text(unCompileJsonVal(decodeURIComponent(values)));
            }

        });

        $("select").each(function(){
            var obj2 =$(this);
            $(this).rightClick(function(){
                var selectVal = obj2.find("option:selected");
                showEditDialog(selectVal);
                selectVal.addClass("EditPosition");
            })

        });

        $("input[type='button']").each(function () {
            $(this).val(unCompileJsonVal(decodeURIComponent(eval($(this).attr("sid")))));
            var currentButton =$(this);
            $(this).rightClick(function(){
                showEditDialog(currentButton,'val');
                currentButton.addClass("EditPosition");
            })


        });


        setTimeout("height_div()",500);
        $("#contents").bind("click",function(){
            height_div();
        });
    });
}


